package org.luxor.commons.playwright.config.properties;

import com.microsoft.playwright.options.Proxy;
import org.luxor.commons.playwright.constant.BrowserEnum;
import org.luxor.commons.playwright.constant.LaunchMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.Arrays;
import java.util.List;


/**
 * playwright 全局配置
 *
 * @author Mr.Yan  @date: 2022/03/21
 */
@ConfigurationProperties(prefix = PlaywrightProperties.PREFIX)
public class PlaywrightProperties {
    public static final String PREFIX = "spring.playwright";

    private final Boolean DEFAULT_ENABLED = Boolean.TRUE;

    public static final BrowserEnum DEFAULT_BROWSER_TYPE_ENUM = BrowserEnum.CHROME;

    public static final LaunchMode DEFAULT_LAUNCH_MODE = LaunchMode.LAUNCH;

    public static final Boolean DEFAULT_HEADLESS = Boolean.FALSE;

    public static final Boolean DEFAULT_SKIP_BROWSER_DOWNLOAD = Boolean.FALSE;

    public static final long DEFAULT_CREATE_BROWSER_TIMEOUT_SECONDS = 12;

    public static final long DEFAULT_CONNECT_TIMEOUT_SECONDS = 3;

    public static final int DEFAULT_RETRY_NUMBER_MAX = 0;

    public static final String DEFAULT_DOWNLOADS_PATH = "/tmp/downloads";

    public static final Boolean DEFAULT_CAMOUFLAGE_ENABLED = Boolean.FALSE;

    /**
     * 是否启用
     */
    private boolean enabled;

    /**
     * 浏览器类型(默认值:chrome)
     */
    private BrowserEnum browser;

    /**
     * 浏览器创建模式 (默认值:LAUNCH，LAUNCH-创建新的浏览器，CONNECT_OVER_CDP-连接已启动的浏览器)
     */
    private LaunchMode launchMode;

    /**
     * 浏览器连接地址(默认值: http://127.0.0.1:9222)
     */
    private List<String> endpoints;

    /**
     * 代理
     */
    @NestedConfigurationProperty
    private Proxy proxy;

    /**
     * 启用无头模式? (默认值:false，true-无界面运行，false-带界面运行)
     */
    private Boolean headless;

    /**
     * 下载目录
     */
    private String downloadsPath;

    /**
     * 浏览器应用路径
     */
    private String executablePath;

    /**
     * 跳过浏览器下载?（默认值:false)
     */
    private Boolean skipBrowserDownload;

    /**
     * 启用伪装功能,绕开反爬特征检测?（默认值:false）
     */
    private Boolean camouflageEnabled;

    /**
     * 创建Browser超时时间(单位秒,默认值:12)
     */
    private long createBrowserTimeoutSeconds;

    /**
     * 连接超时时间(单位秒,默认值:12)
     */
    private long connectTimeoutSeconds;

    /**
     * 重试次数限制(默认值:0,不允许重试)
     */
    private int retryNumMax;

    public PlaywrightProperties() {
        this.enabled = DEFAULT_ENABLED.booleanValue();
        this.browser = DEFAULT_BROWSER_TYPE_ENUM;
        this.launchMode = DEFAULT_LAUNCH_MODE;
        this.endpoints = Arrays.asList("http://127.0.0.1:9222");
        this.headless = DEFAULT_HEADLESS;
        this.downloadsPath = DEFAULT_DOWNLOADS_PATH;
        this.skipBrowserDownload = DEFAULT_SKIP_BROWSER_DOWNLOAD;
        this.camouflageEnabled = DEFAULT_CAMOUFLAGE_ENABLED;
        this.createBrowserTimeoutSeconds = DEFAULT_CREATE_BROWSER_TIMEOUT_SECONDS;
        this.connectTimeoutSeconds = DEFAULT_CONNECT_TIMEOUT_SECONDS;
        this.retryNumMax = DEFAULT_RETRY_NUMBER_MAX;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public BrowserEnum getBrowser() {
        return browser;
    }

    public void setBrowser(BrowserEnum browser) {
        this.browser = browser;
    }

    public LaunchMode getLaunchMode() {
        return launchMode;
    }

    public void setLaunchMode(LaunchMode launchMode) {
        this.launchMode = launchMode;
    }

    public List<String> getEndpoints() {
        return endpoints;
    }

    public void setEndpoints(List<String> endpoints) {
        this.endpoints = endpoints;
    }

    public Boolean getHeadless() {
        return headless;
    }

    public void setHeadless(Boolean headless) {
        this.headless = headless;
    }

    public Proxy getProxy() {
        return proxy;
    }

    public void setProxy(Proxy proxy) {
        this.proxy = proxy;
    }

    public Boolean getSkipBrowserDownload() {
        return skipBrowserDownload;
    }

    public void setSkipBrowserDownload(Boolean skipBrowserDownload) {
        this.skipBrowserDownload = skipBrowserDownload;
    }

    public Boolean getCamouflageEnabled() {
        return camouflageEnabled;
    }

    public void setCamouflageEnabled(Boolean camouflageEnabled) {
        this.camouflageEnabled = camouflageEnabled;
    }

    public long getCreateBrowserTimeoutSeconds() {
        return createBrowserTimeoutSeconds;
    }

    public void setCreateBrowserTimeoutSeconds(long createBrowserTimeoutSeconds) {
        this.createBrowserTimeoutSeconds = createBrowserTimeoutSeconds;
    }

    public long getConnectTimeoutSeconds() {
        return connectTimeoutSeconds;
    }

    public void setConnectTimeoutSeconds(long connectTimeoutSeconds) {
        this.connectTimeoutSeconds = connectTimeoutSeconds;
    }

    public int getRetryNumMax() {
        return retryNumMax;
    }

    public void setRetryNumMax(int retryNumMax) {
        this.retryNumMax = retryNumMax;
    }

    public String getDownloadsPath() {
        return downloadsPath;
    }

    public void setDownloadsPath(String downloadsPath) {
        this.downloadsPath = downloadsPath;
    }

    public String getExecutablePath() {
        return executablePath;
    }

    public void setExecutablePath(String executablePath) {
        this.executablePath = executablePath;
    }
}
